package com.orientation.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orientation.commom.Result;
import com.orientation.entity.Building;
import com.orientation.service.BuildingService;
import com.orientation.vo.BuildingRoomVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
 * <p>
 * 报到 前端控制器
 * </p>
 *
 * @author xuandian
 * @since 2022-3-9
 */
@Api(tags = "宿舍楼栋")
@Validated
@RestController
@RequestMapping("/building")
public class BuildingController {
    @Resource
    private BuildingService buildingService;

    @ApiOperation("添加")
    @PostMapping
    public ResponseEntity<Result> insertBuild(@RequestBody Building building) {
        buildingService.insertBuild(building);
        return ResponseEntity.ok(new Result());
    }

    @ApiOperation("修改")
    @PutMapping
    public ResponseEntity<Result> updateBuilding(@RequestBody Building building) {
        buildingService.updateBuilding(building);
        return ResponseEntity.ok(new Result());
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable String id) {
        return buildingService.removeById(id);
    }

    @ApiOperation("查询单条")
    @GetMapping("/{id}")
    public Building findById(@PathVariable String id) {
        return buildingService.getById(id);
    }

    @ApiOperation("查询所有")
    @GetMapping
    public List<Building> findAll() {
        return buildingService.list();
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public ResponseEntity<Result> queryPage(@RequestParam(required = false, defaultValue = "") String region,
                                            @RequestParam(required = false, defaultValue = "") String name,
                                            @RequestParam(required = false, defaultValue = "") String type,
                                            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                            @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        // return collegeService.page(new Page<>(pageNum, pageSize), Wrappers.<College>lambdaQuery().like(StringUtils.isNotBlank(name), College::getCollegeName, name));
        IPage<Building> buildingIPage = buildingService.getBuilding(pageNum, pageSize, region, name, type);
        return ResponseEntity.ok(Result.success(buildingIPage));

    }

    @ApiOperation("分组分页查询楼栋")
    @GetMapping("/queryBuildingVo")
    public ResponseEntity<Result> queryBuildingVo(@RequestParam(required = false, defaultValue = "") String region,
                                                  @RequestParam(required = false, defaultValue = "") String name,
                                                  @RequestParam(required = false, defaultValue = "") String type,
                                                  @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                  @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        Page<BuildingRoomVo> buildingRoomVoPage = buildingService.queryBuildingVo(pageNum, pageSize, region, name, type);
        return ResponseEntity.ok(Result.success(buildingRoomVoPage));

    }

    @ApiOperation("查询楼栋")
    @GetMapping("/createBuildingRoom")
    public ResponseEntity<Result> queryBuildingVo(@RequestParam(required = false, defaultValue = "") String region,
                                                  @RequestParam(required = false, defaultValue = "一公寓") String name,
                                                  @RequestParam(required = false, defaultValue = "") String type) {
        BuildingRoomVo buildingRoom = buildingService.createBuildingRoom(region, name, type);
        return ResponseEntity.ok(Result.success(buildingRoom));

    }

    @ApiOperation("查询楼信息编辑")
    @GetMapping("/createBuildingRoomFloor")
    public ResponseEntity<Result> createBuildingRoomFloor(@RequestParam(required = false, defaultValue = "") String region,
                                                          @RequestParam(required = false, defaultValue = "一公寓") String name,
                                                          @RequestParam(required = false, defaultValue = "") String type,
                                                          @RequestParam(value = "floor") String floor) {
        BuildingRoomVo buildingRoom = buildingService.createBuildingRoom(region, name, type);

        buildingRoom.setBuildingList(buildingRoom.getBuildingList().stream().filter((Building s) -> s.getFloor().equals(floor)).collect(Collectors.toList()));
        return ResponseEntity.ok(Result.success(buildingRoom));

    }

    @ApiOperation("查询楼栋全部")
    @GetMapping("/getAll")
    public ResponseEntity<Result> getAll() {
        return ResponseEntity.ok(Result.success(buildingService.getAll()));

    }

    @ApiOperation("查询楼栋按照名去重（下拉框）")
    @GetMapping("/getAllName")
    public ResponseEntity<Result> getAllName() {
        ArrayList<Building> collect = buildingService.getAll().stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(c -> c.getName()))), ArrayList::new));
        return ResponseEntity.ok(Result.success(collect));

    }

    @ApiOperation("按照类型查询楼栋全部")
    @GetMapping("/getBuildingByType")
    public ResponseEntity<Result> getBuildingByType(@RequestParam(required = true, value = "type", defaultValue = "男生公寓") String type) {
        // List<Building> all = buildingService.getAll();
        // List<Building> collect = new ArrayList<>();
        // List<Building> buildings = new ArrayList<>();
        // if (!CollectionUtils.isEmpty(all)) {
        //     collect = all.stream().filter((Building b) -> type.equals(b.getType())).collect(Collectors.toList());
        //     buildings = collect.stream().filter((Building b) -> b.getRoom() != null && b.getRoom() > 0).collect(Collectors.toList());
        // }
        List<Building> buildingList = buildingService.queryBuildingType(type);
        return ResponseEntity.ok(Result.success(buildingList));

    }


    @ApiOperation("按照类型查询楼栋全部名")
    @GetMapping("/queryAreaList")
    public ResponseEntity<Result> queryAreaList(@RequestParam(required = true, value = "type", defaultValue = "男生公寓") String type) {
        // List<String> buildingNames = buildingService.queryAreaList(type);
        List<Building> buildingList = buildingService.queryBuildingType(type);
        buildingList = buildingList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(c -> c.getName()))), ArrayList::new));
        return ResponseEntity.ok(Result.success(buildingList));

    }

    @ApiOperation("根据公寓名称查询")
    @GetMapping("/queryAreaListByName")
    public ResponseEntity<Result> queryAreaListByName(@RequestParam(required = true, value = "type", defaultValue = "男生公寓") String type,
                                                      @RequestParam(required = true, value = "name", defaultValue = "男生公寓") String name) {
        List<Building> buildingList = buildingService.queryBuildingType(type);
        buildingList = buildingList.stream().filter(b -> b.getName().equals(name)).collect(Collectors.toList());
        return ResponseEntity.ok(Result.success(buildingList));

    }
}

